home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 493.lha / SmallIFFParseLibrary / sources / libsup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-06  |  5.5 KB  |  254 lines

  1. /* mylib.c    -- exec library routines for mylib     */
  2.  
  3. #include <exec/lists.h>
  4. #include <exec/resident.h>
  5. #include <graphics/text.h>
  6. #include <intuition/intuition.h>
  7. #include <intuition/intuitionbase.h>
  8. #include "undefs.h"
  9. #include "handleiff.h"
  10.  
  11. void myInit(void);
  12. long myOpen(long d);
  13. long myClose(long d);
  14. long myExpunge(long d);
  15.  
  16. #pragma amicall(IFFParseBase, 0x06, myOpen(d0))
  17. #pragma amicall(IFFParseBase, 0x0c, myClose(d0))
  18. #pragma amicall(IFFParseBase, 0x12, myExpunge(d0))
  19.  
  20. /* The actual library. */
  21. struct IFFParseBase {
  22.     struct Library Lib;
  23.     ULONG   SegList;
  24. };
  25.  
  26. /* library initialization table, used for AUTOINIT libraries            */
  27. struct InitTable {
  28.     unsigned long    it_DataSize;          /* library data space size        */
  29.     void            **it_FuncTable;          /* table of entry points            */
  30.     void             *it_DataInit;          /* table of data initializers        */
  31.     void            (*it_InitFunc)(void); /* initialization function to run    */
  32. };
  33.  
  34. void *libfunctab[] = {    /* my function table */
  35.     myOpen,                    /* standard open    */
  36.     myClose,                /* standard close    */
  37.     myExpunge,                /* standard expunge    */
  38.     0,
  39.  
  40. /*------ Basic functions ------*/
  41.     AllocIFF, 
  42.     OpenIFF, 
  43.     ParseIFF, 
  44.     CloseIFF, 
  45.     FreeIFF, 
  46. /*------ Read/Write functions ------*/
  47.     ReadChunkBytes, 
  48.     WriteChunkBytes, 
  49.     ReadChunkRecords, 
  50.     WriteChunkRecords, 
  51. /*------ Context entry/exit ------*/
  52.     PushChunk, 
  53.     PopChunk, 
  54.     Reserved, /*--- (1 function slot reserved here) ---*/
  55. /*------ Low-level handler installation ------*/
  56.     EntryHandler, 
  57.     ExitHandler, 
  58. /*------ Built-in chunk/property handlers ------*/
  59.     PropChunk, 
  60.     PropChunks, 
  61.     StopChunk, 
  62.     StopChunks, 
  63.     CollectionChunk, 
  64.     CollectionChunks, 
  65.     StopOnExit, 
  66. /*------ Context utilities ------*/
  67.     FindProp, 
  68.     FindCollection, 
  69.     FindPropContext, 
  70.     CurrentChunk, 
  71.     ParentChunk, 
  72. /*------ LocalContextItem support functions ------*/
  73.     AllocLocalItem, 
  74.     LocalItemData, 
  75.     SetLocalItemPurge, 
  76.     FreeLocalItem, 
  77.     FindLocalItem, 
  78.     StoreLocalItem, 
  79.     StoreItemInContext, 
  80. /*------ IFFHandle initialization ------*/
  81.     InitIFF, 
  82.     InitIFFasDOS, 
  83.     InitIFFasClip, 
  84. /*------ Internal clipboard support ------*/
  85.     OpenClipboard, 
  86.     CloseClipboard, 
  87. /*------ Miscellaneous ------*/
  88.     GoodID, 
  89.     GoodType, 
  90.     IDtoStr, 
  91.                         
  92.     (void *)-1                /* end of function vector table */
  93. };
  94.  
  95. struct InitTable myInitTab =  {
  96.     sizeof (struct IFFParseBase),
  97.     libfunctab,
  98.     0,                        /* will initialize my data in funkymain()    */
  99.     myInit
  100. };
  101.  
  102. #define MYREVISION    40        /* would be nice to auto-increment this        */
  103.  
  104. char myname[] = "iffparse.library";
  105. char myid[] = "Subset of the iffparse.library - By Michael Jansson\r\n";
  106.  
  107. extern struct Resident    myRomTag;
  108.  
  109.  
  110. long
  111. _main(struct IFFParseBase *IFFParseBase, unsigned long seglist)
  112. {
  113.     IFFParseBase->SegList = seglist;
  114.  
  115.     geta4();
  116.     TraceCall;
  117.     /* ----- init. library structure  -----        */
  118.     IFFParseBase->Lib.lib_Node.ln_Type = NT_LIBRARY;
  119.     IFFParseBase->Lib.lib_Node.ln_Name = (char *) myname;    
  120.     IFFParseBase->Lib.lib_Flags = LIBF_SUMUSED | LIBF_CHANGED;
  121.     IFFParseBase->Lib.lib_Version = myRomTag.rt_Version;
  122.     IFFParseBase->Lib.lib_Revision = MYREVISION;
  123.     IFFParseBase->Lib.lib_IdString = (APTR) myid;
  124.  
  125.     TraceCall;
  126.     return 1L;
  127. }
  128.  
  129. #ifdef SAFE_OPEN
  130.  
  131. struct TextAttr safeFont = {
  132.     (UBYTE *)"topaz.font", 
  133.     9,
  134.     FS_NORMAL, 
  135.     FPF_ROMFONT
  136. };
  137.  
  138. struct IntuiText Body = {
  139.     -1, -1, 
  140.     JAM1, 
  141.     36, 16, 
  142.     NULL, 
  143.     (UBYTE *)"Use Small-IFFParse.library?",
  144.     NULL
  145. };
  146.  
  147. struct IntuiText Positive = {
  148.     -1, -1,
  149.     0, 
  150.     7, 4, 
  151.     NULL, 
  152.     (UBYTE *)"Do it!", 
  153.     NULL
  154. };
  155.  
  156. struct IntuiText Negative = {
  157.     -1, -1, 
  158.     JAM1, 
  159.     7, 4, 
  160.     &safeFont, 
  161.     (UBYTE *)"Oh no!",
  162.     NULL
  163. };
  164.  
  165. IBase *IntuitionBase;
  166. #endif
  167.  
  168. long
  169. myOpen(long d)
  170. {
  171.     struct IFFParseBase *IFFParseBase;
  172.  
  173.     geta4();
  174.     TraceCall;
  175. #ifdef SAFE_OPEN
  176.     long response = TRUE;
  177.  
  178.     /* Open some other libraries. */
  179.     if ((IntuitionBase=(IBase *)OpenLibrary("intuition.library",0L))==NULL) {
  180.         return 0L;
  181.     }
  182.     response = AutoRequest(NULL, 
  183.                            &Body, &Positive, &Negative,
  184.                            VANILLAKEY, 0UL, 
  185.                            320L, 80L);
  186.     CloseLibrary(IntuitionBase);
  187.     if (response==FALSE)
  188.         return 0L;
  189. #endif
  190.  
  191.     /* mark us as having another customer                    */
  192.     IFFParseBase->Lib.lib_OpenCnt++;
  193.  
  194.     /* prevent delayed expunges (standard procedure)        */
  195.     IFFParseBase->Lib.lib_Flags &= ~LIBF_DELEXP;
  196.  
  197.     TraceCall;
  198.     return ((long) IFFParseBase);
  199. }
  200.  
  201. long
  202. myClose(long d)
  203. {
  204.     struct IFFParseBase *IFFParseBase;
  205.     long retval = 0;
  206.  
  207.  
  208.     geta4();
  209.     TraceCall;
  210.     if (--IFFParseBase->Lib.lib_OpenCnt == 0) {
  211.  
  212.         if  (IFFParseBase->Lib.lib_Flags & LIBF_DELEXP) {
  213.             /* no more people have me open,
  214.              * and I have a delayed expunge pending
  215.              */
  216.             retval = myExpunge(0); /* return segment list    */
  217.         }
  218.     }
  219.  
  220.     TraceCall;
  221.     return (retval);
  222. }
  223.  
  224. long
  225. myExpunge(long d)
  226. {
  227.     struct IFFParseBase *IFFParseBase;
  228.     unsigned long seglist = 0;
  229.     long libsize;
  230.     extern struct Library *DOSBase;
  231.  
  232.     geta4();
  233.     TraceCall;
  234.     if (IFFParseBase->Lib.lib_OpenCnt == 0) {
  235.         /* really expunge: remove libbase and freemem    */
  236.  
  237.         seglist    = IFFParseBase->SegList;
  238.  
  239.         Remove(&IFFParseBase->Lib.lib_Node);
  240.                                 /* i'm no longer an installed library    */
  241.  
  242.         libsize = IFFParseBase->Lib.lib_NegSize+IFFParseBase->Lib.lib_PosSize;
  243.         FreeMem((char *)IFFParseBase-IFFParseBase->Lib.lib_NegSize, libsize);
  244.         CloseLibrary(DOSBase);        /* keep the counts even */
  245.  
  246.     }
  247.     else
  248.         IFFParseBase->Lib.lib_Flags |= LIBF_DELEXP;
  249.  
  250.     /* return NULL or real seglist                */
  251.     TraceCall;
  252.     return ((long) seglist);
  253. }
  254.